#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
爬取工作信息
script name: find_job
Created on 2020/3/10 23:36 by PyCharm
@author: fujianjian
"""
import requests


class LagouCrawler:
    __lagou_job_uri__ = 'https://www.lagou.com/jobs/positionAjax.json'

    __lagou_headers__ = {
        'Referer':'https://www.lagou.com/jobs/list_java/p-city_3?&cl=false&fromSearch=true&labelWords=&suginput=',
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4077.0 Safari/537.36',
        'Cookie':'user_trace_token=20200129222100-9c4bc892-5d24-4bb0-9035-18ba5086f252; _ga=GA1.2.627153304.1580307661; LGUID=20200129222100-92faeffd-42a2-11ea-8632-525400f775ce; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2216ff1ad4d69127-0fe284b8c2891d-6835047e-1296000-16ff1ad4d6a7cf%22%2C%22%24device_id%22%3A%2216ff1ad4d69127-0fe284b8c2891d-6835047e-1296000-16ff1ad4d6a7cf%22%7D; LG_LOGIN_USER_ID=1d7cf69931b0727868c039abd48f69086c5974c4fe2e4644; LG_HAS_LOGIN=1; JSESSIONID=ABAAABAABFIAAAC1367CE4D94E1EC68E180508283230EDC; WEBTJ-ID=20200310180544-170c3e848af87-0e722bbb3d289c-6a31007d-1296000-170c3e848b0ae4; _gid=GA1.2.1640974919.1583834745; index_location_city=%E5%85%A8%E5%9B%BD; TG-TRACK-CODE=index_search; X_MIDDLE_TOKEN=b44660d56482800c912a7e8e40656665; _gat=1; PRE_UTM=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2F; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1583834727,1583834745,1583854861; LGSID=20200310234101-d723e051-7c3c-4673-94a6-5dd3c2c01e03; PRE_HOST=www.baidu.com; PRE_SITE=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DQL7PW5sPoT42xQIaXm6SMZ0fGbAISjU7vtP%5FHbbOfA%5F%26wd%3D%26eqid%3Ddc948bfe0004e935000000055e67b509; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1583855045; LGRID=20200310234443-bff7c7e2-8ac1-4537-a650-818853131c5e; X_HTTP_TOKEN=d5c5bff50d071e4072155838510808def7895b67d6; SEARCH_ID=f6b765a082794bf597dd191f3ead17db'
    }

    def crawler_job_data(self, jobword, city='全国'):
        """
        工作关键字
        :param jobword:
        :return:
        """
        url = self.__lagou_job_uri__
        if city != '全国':
            url += '?city={}&needAddtionalResult=false'.format(city)
        params = {
            'first': 'true',
            'pn': 1,
            'kd': 'java'
        }
        print('fetch job url', url)
        res = requests.post(url, data=params, headers=self.__lagou_headers__)
        print('res', res.text)


if __name__ == '__main__':
    lc = LagouCrawler()
    lc.crawler_job_data('java', city='上海')
